package linkedlist;

import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListDemo {
    public static void main(String[] args) {
        LinkedList<Integer> myLinkedList = new LinkedList<>();
        myLinkedList.add(1);
        myLinkedList.add(2);
        myLinkedList.add(3);
        myLinkedList.add(4);
        myLinkedList.add(5);
        myLinkedList.add(6);
        myLinkedList.add(7);
        //foreach遍历
        for(int e:myLinkedList){
            System.out.print(e+" ");
        }
        System.out.println();
        //使用迭代器遍历
        ListIterator<Integer> it = myLinkedList.listIterator();
        while(it.hasNext()){
            System.out.print(it.next()+" ");
        }
        //使用反向迭代器---反向遍历
        ListIterator<Integer> it2 = myLinkedList.listIterator(myLinkedList.size());
        while(it2.hasPrevious()){
            System.out.print(it.previous()+" ");
        }
    }
}
